<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="queryParams.keyword"
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="sousuo"
        />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          node-key="id"
          :data="depts"
          :props="defaultProps"
          highlight-current
          default-expand-all
          :expand-on-click-node="false"
          @current-change="setNode"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary">添加员工</el-button>
          <el-button size="mini">excel导入</el-button>
          <el-button size="mini">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="list">
          <el-table-column label="头像" align="center" prop="staffPhoto">
            <template v-slot="{row}">
              <el-avatar v-if="row.staffPhoto" :size="30" :src="row.staffPhoto" />
              <span v-else class="username">{{ row.username }}</span>
            </template>
          </el-table-column>
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="手机号" sortable prop="mobile" />
          <el-table-column label="工号" sortable prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template v-slot="{row}">
              {{ row.formOfEmployment ===1 ? '正式': '非正式' }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" />
          <el-table-column label="操作">
            <template>
              <el-button type="text" size="mini">查看</el-button>
              <el-button type="text" size="mini">编辑</el-button>
              <el-button type="text" size="mini">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" justify="end">
          <el-pagination
            :page-size="queryParams.pagesize"
            :current-page="queryParams.page"
            layout="total,prev, pager, next"
            :total="total"
            @current-change="pagechange"
          />
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import { getYuangongList } from '@/api/employee'
import { getDepartment } from '@/api/department'
import { transitionTree } from '@/utils'
export default {
  name: 'Employee',
  data() {
    return {
      depts: [],
      defaultProps: {
        label: 'name'
      },
      queryParams: {
        departmentId: null,
        page: 1,
        pagesize: 10,
        keyword: ''
      },
      list: [],
      total: 0
    }
  },
  created() {
    this.getbumen()
  },
  methods: {
    async getbumen() {
      this.depts = transitionTree(await getDepartment(), 0)
      this.queryParams.departmentId = this.depts[0].id
      this.$nextTick(() => {
        this.$refs.tree.setCurrentKey(this.queryParams.departmentId)
      })
      this.getyuangong()
    },
    setNode(node) {
      this.queryParams.departmentId = node.id
      this.queryParams.page = 1
      this.getyuangong()
    },
    async getyuangong() {
      const { rows, total } = await getYuangongList(this.queryParams)
      this.list = rows
      this.total = total
    },
    pagechange(newpage) {
      this.queryParams.page = newpage
      this.getyuangong()
    },
    sousuo() {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.queryParams.page = 1
        this.getyuangong()
      }, 200)
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
